<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <title>商品后台管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
    <link rel="stylesheet" href="./css/bootstrap.css">
    <link rel="stylesheet" href="./css/all.css">
    <link rel="stylesheet" href="./css/course_category.css">
</head>

<body>
    <div id="app">
        <nav class="navbar">
            <div class="container">
                <div class="navbar-header">
                    <a class="navbar-brand" href="#">
                        <img src="image/easy-pay.png">
                    </a>
                </div>
                <ul class="nav navbar-nav navbar-right" style="margin-top: 30px">
                    <li><a href="#"><span class="inner">首页</span></a></li>
                    <li>
                        <a href="#">
                            <span class="inner">商品管理</span></a>
                    </li>
                    <li>
                        <a href="#">
                            <span class="inner">购物车管理</span>
                        </a>
                    </li>

                    <li>
                        <a href="#">
                            <span class="inner">用户管理</span></a>
                    </li>
                </ul>

            </div><!-- /.container-fluid -->
        </nav>
        <div style="min-height: 400px">
            <div class="container">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h4>商品管理-商品列表</h4>
                    </div>
                    <div class="panel-body">
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th style="width: 15%" class="text-center">商品编号</th>
                                    <th style="width: 65%" class="text-center">商品标题</th>
                                    <th style="width: 20%" class="text-center">编辑</th>
                                </tr>
                            </thead>
                            <tbody id="mytbody">
                                <!-- 在此处使用v-for遍历数据 -->

                            </tbody>
                        </table>
                        <div>
                            <button class="btn btn-primary">添加商品</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!-- 在此处完善页脚 -->
        <footer>
            <div class="container">
                <p class="text-center">易购集团</p>
                <p class="text-center">Copyright 2019</p>
            </div>
        </footer>
    </div>
</body>

</html>
<script src="./js/axios.min.js"></script>
<script>
    //初始化界面（封装）
    function init() {
        axios.post('http://localhost:8090/h8/goods/list')
            .then(res => {
                // console.log(res.data);
                if (res.data.success) {
                    // 获取表格
                    const mytbody = document.getElementById('mytbody')
                    //清空表格原有数据
                    mytbody.innerHTML = ''
                    // 通过遍历创建表格的行
                    let list = res.data.list
                    for (let i in list) {
                        let tr = `
                <tr>
                                    <td class="text-center">${list[i].id}</td>
                                    <td class="text-left">${list[i].title}</td>
                                    <td class="text-center">
                                        <button class="btn btn-primary btn-sm">详细</button>
                                        <button class="btn btn-primary btn-sm">编辑</button>
                                        <button class="btn btn-primary btn-sm" onclick="deleteGoods(${list[i].id})">删除</button>
                                    </td>
                                </tr>
                `
                        mytbody.innerHTML += tr
                    }
                }
            })
    }
    init()
    //删除
    function deleteGoods(id) {
        axios.post('http://localhost:8090/h8/goods/deleteGoods', { id })
            .then(res => {
                // alert(res.data.msg)
                init()
            })
    }
</script>